package com.moon.game.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.moon.game.resource.model.Animal;

/**
 * @description 珍兽资源管理
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-13 下午2:14:53
 */
public class AnimalMG {
	private static final Logger log = Logger.getLogger(AnimalMG.class);
	/**所有的珍兽资源列表*/
	private static ConcurrentHashMap<Integer,Animal> animalsRes = new ConcurrentHashMap<Integer, Animal>();
	/**凡兽资源列表*/
	private static ArrayList<Animal> commonAnimalsRes = new ArrayList<Animal>();
	/**仙兽资源列表*/
	private static ArrayList<Animal> immortalAnimalsRes = new ArrayList<Animal>();
	/**神兽资源列表*/
	private static ArrayList<Animal> godAnimalsRes = new ArrayList<Animal>();
	
	
	/**获取完整的珍兽资源*/
	public static ConcurrentHashMap<Integer, Animal> getAnimalsRes() {
		return animalsRes;
	}
	/** 根据id获取animal对象 */
	public final Animal getAnimal(int id) {
		return animalsRes.get(id);
	}
	/**获取凡兽列表*/
	public final List<Animal> getCommonAnimalList() {
		return commonAnimalsRes;
	}
	/**获取仙兽列表*/
	public final List<Animal> getImmortalAnimalList() {
		return immortalAnimalsRes;
	}
	/**获取神兽列表*/
	public final List<Animal> getGodAnimalList() {
		return godAnimalsRes;
	}

	private AnimalMG() {
	}
	public static AnimalMG instance = new AnimalMG();
	public void init() {
		log.debug("load animals start...");
		loadAnimal();
		log.debug("load animals succ...");
	}
	
	private void loadAnimal() {
		List<Animal> animals = DBManager.queryAllAnimals();
		for (Animal animal : animals) {
			animalsRes.put(animal.getId(), animal);
			switch(animal.getHierarchy()) {
				case 0:
					commonAnimalsRes.add(animal);
					break;
				case 1:
					immortalAnimalsRes.add(animal);
					break;
				case 2:
					godAnimalsRes.add(animal);
					break;
			}
		}
	}
	
	
	/**
	 * 珍兽工厂
	 * 
	 * @param animalId
	 * @return
	 */
	public Animal animalFactory(int animalId) {
		Animal animal = new Animal();
		Animal animalRes = getAnimal(animalId);
//		Utils.copyProperties(animal, animalRes);
		animal.setChildId(animalRes.getChildId());
		animal.setDescription(animalRes.getDescription());
		animal.setEggId(animalRes.getEggId());
		animal.setEndTime(animalRes.getEndTime());
		animal.setHierarchy(animalRes.getHierarchy());
		animal.setId(animalRes.getId());
		animal.setName(animalRes.getName());
		animal.setStartTime(animalRes.getStartTime());
		return animal;
	}
}
